﻿
//!~ change to your namesapce
using System;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.IO;
namespace MyWebToolingCollection
{
    class CodingOrEncodingMethods
    {
        public static string encodingAllTypeBy(int type, string text)
        {
            string chgOut = string.Empty;
            switch (type)
            {
            //MainHexadecimalItems 十六进制数处理
                case 11:
                    chgOut = HexadecimalCodingToString(text);
                    break;
                case 12:

                    break;
                case 13:

                    break;

           //MainBinaryNumbers 二进制数处理
                case 21:

                    break;
                case 22:

                    break;

           //MainBaseSixtyFour Base64编码串处理
                case 31:
                    chgOut = StringToBaseSixtyFourCode(text);
                    break;
                case 32:
                    chgOut = BaseSixtyFourCodeToString(text);
                    break;

            //MainStringEncoding 常规字符串处理
                case 41:
                    chgOut = stringToHexadecimalCoding(text);
                    break;
                case 42:

                    break;

            //默认不处理
                default:
                    break;
            }

            return chgOut;
        }


        /*
        *  把中文字符串转换为十六进制Unicode编码字符串
         *  http://my.oschina.net/dexterman/blog/285374
        * */
        public static string stringToHexadecimalCoding(string text)
        {
            string chgOut = string.Empty;
            Char[] values = text.ToCharArray();

            foreach (char letter in values)
            {
                // Get the integral value of the character.
                Int64 val = Convert.ToInt64(letter);
                // Convert the decimal value to a hexadecimal value in string form.
                string hexOutput = string.Format("X", val);
                if (hexOutput.Length > 2)
                {
                    chgOut += hexOutput.Substring(0, 2) + " " + hexOutput.Substring(2, 2) + " ";//手动插入空格
                }
                else
                    chgOut += hexOutput + " ";//手动补上空格
            }
            return chgOut;
        }


        /*
        *  把十六进制Unicode编码字符串转换为中文字符串
        * */
        public static string HexadecimalCodingToString(string str)
        {
            //string str = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
            string chgOut = string.Empty;
            string[] hexValuesSplit = str.Split(' ');
            foreach (String hex in hexValuesSplit)
            {
                // Convert the number expressed in base-16 to an integer.
                int value = Convert.ToInt32(hex, 16);
                // Get the character corresponding to the integral value.
                string stringValue = Char.ConvertFromUtf32(value);
                /*
                char charValue = (char)value;
                Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
                hex, value, stringValue, charValue);
                */
                chgOut += stringValue;
            }
            return chgOut;
        }
        

        /*
        *  把字符串转换为Base64编码串
        * */
        public static string StringToBaseSixtyFourCode(string str)
        {
            string chgOut = string.Empty;

            //byte[] bytes = Encoding.Default.GetBytes(str);
            Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
            chgOut = Convert.ToBase64String(bytes);

            return chgOut;
        }


        /*
        *  解码Base64编码串
        * */
        public static string BaseSixtyFourCodeToString(string str)
        {
            string chgOut = string.Empty;

            byte[] outputb = Convert.FromBase64String(str);
            //chgOut = Encoding.Default.GetString(outputb);
            chgOut = System.Text.Encoding.UTF8.GetString(outputb);

            return chgOut;
        }


        /*
         * 图片转成Base64编码串
        */
        public static string ImageFilesToBaseSixtyFourCode(Image imgaes, string str)
        {
            Image img = imgaes;
            BinaryFormatter binFormatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            binFormatter.Serialize(memStream, img);
            byte[] bytes = memStream.GetBuffer();
            string baseCode = Convert.ToBase64String(bytes);

            return baseCode;
        }


        /*
         * 解码Base64编码图片
        */
        public static Image BaseSixtyFourCodeToImage(string str)
        {
            string base64 = str;

            byte[] bytes = Convert.FromBase64String(base64);
            MemoryStream memStream = new MemoryStream(bytes);
            BinaryFormatter binFormatter = new BinaryFormatter();
            Image img = (Image)binFormatter.Deserialize(memStream);

            //this.pictureBoxTabThree.Image = img;
            return img;
        }
    }
}